package com.example.musicplayer.mycontroller;
import java.util.*;


import android.view.*;

import com.example.musicplayer.model.Video;
import com.example.musicplayer.myenum.EnumPlayState;
import com.example.musicplayer.util.MediaListUtil;
import com.example.musicplayer.util.MyPageUtil;

public class VideoController
{
	
	MediaListUtil videoListUtil;
	Video video;
	MyPageUtil myPageUtil;
	private static VideoController videoController;

	private VideoController(Video video){

		this.video=video;
		videoListUtil=new MediaListUtil();
		myPageUtil=new MyPageUtil();
	}
	
	public static synchronized VideoController getInstance(Video video){
		
		if(videoController==null){
			videoController=new VideoController(video);
		}
		return videoController;
	}
	
	
	public ArrayList<String> getVideoList(String ...args){
		ArrayList<String> videoList=null;
		if(args.length>0){
			videoList = videoListUtil.getNameList(MediaListUtil.TYPE_VIDEO,args[0]);
		}else{
			videoList = videoListUtil.getNameList(MediaListUtil.TYPE_VIDEO);
		}
		myPageUtil.setIndex(0);
		myPageUtil.setTotalCount(videoListUtil.getCount());
		return videoList;
	}
	
	public String getVideoPath(int index){
		
		return videoListUtil.getFilePathByIndex(index);
	}
	
	public void setMusicIndex(int index){
		
		try
		{
			myPageUtil.setIndex(index);
			video.setPath(videoListUtil.getFilePathByIndex(index));
		}
		catch (Exception e)
		{
			playNextMusic();
		}

	}

	public void setSeekTime(int time){
		video.setSeekTime(time);
	}
	
	public int getSeekTime(){
		return video.getSeekTime();
	}

	public void setDisplay(SurfaceHolder surfaceHolder){
		video.setDisplay(surfaceHolder);
	}
	
	public void playMusic(){

		if(video.getState()==EnumPlayState.PLAY){
			video.pause();
		}else if(video.getState()== EnumPlayState.PAUSE){
			video.play();
		}else if(video.getState()==EnumPlayState.STOP){
			video.play();
		}

	}

	public void playNextMusic(){
		int index=myPageUtil.next();
		setMusicIndex(index);
		video.play();
	}

	public void playPreMusic(){
		int index=myPageUtil.previous();
		setMusicIndex(index);
		video.play();
	}
	
	public void stop(){
		video.stop();
	}
	
	public void pause(){
		video.pause();
	}
	
}
